'''
Created on 29/09/2013

@author: anonymous
'''

from DocumentCollection import DocumentCollection
from SearchManager import SearchManager
from InvertedIndex import InvertedIndex

class BooleanSearch:
    searchManager = SearchManager()
    invertedIndex= InvertedIndex()
    documentCollection = DocumentCollection("")
    
    def __init__(self):
        pass
        
    def executeSearch(self,searchValue):
        #re indexing documents
        documents = self.documentCollection.collectDocuments();
        for document in documents:
            tokens = self.documentCollection.getTokens(document)
            tokens =  self.documentCollection.normalizeTokens(tokens)
            self.invertedIndex.addWordsDocument(tokens, document)
        #executing search
        if not searchValue or searchValue.endswith("-"):
            self.documentCollection.writeSearchResult("response.html", [])
            return
        parsedInput = self.searchManager.inputParser(searchValue)
        postfix = self.searchManager.infixToPostfix(parsedInput)
        docIDs = self.searchManager.postfixEvaluation(postfix,self.invertedIndex)
        docNames = self.invertedIndex.getDocumentsNames(docIDs)
        #writing output
        self.documentCollection.writeSearchResult("response.html", docNames)
        